using UnityEngine;
using System.Collections;
using System;
using System.IO;

public class Gun : MonoBehaviour 
{
    public string GunName = "";

    public enum ShootingType { Automatic = 0, Manual = 1, Semi_Automatic = 2, bazoka = 3 }
    public enum optic { IronSight = 0, reflex = 1, ACOG = 2, EOTech = 3, NVG = 4, RifleScope = 5 }
    public enum tactical { none = 0, suppressor = 1, tacticalLight = 2, GrenadLauncher = 3, Bayonet = 4 }

    public bool usingGun = false;
    public float fireDelay = 1.0f;
    public string fireAnimation, reloadAnimation;
    public string IronSightAnim, reflexAnim, ACOGAnim, EOTechAnim, NVGAnim, RifeScopeAnim;
    public float bulletDistance = 2000, maxMagAmo = 7, damage = 20;
    public GameObject ScopePosition, TacticlePosition, bulletShell, muzzleFlash;
    public ShootingType type = ShootingType.Manual;
    public optic currentOptic = optic.IronSight;
    public tactical currentTactic = tactical.none;
    public string currentCamo = "", aimAnimation;
    public float currentAmmo = 0;
    public GameObject model, player;
    public GameObject[] rendererObjects;
    public GameObject zoomLookAt;

    private void Start()
    {
        //Assign renderer Objects
        rendererObjects = new GameObject[model.GetComponentsInChildren<Renderer>().Length];
        for (int x = 0; x < model.GetComponentsInChildren<Renderer>().Length; x++)
        {
            rendererObjects[x] = model.GetComponentsInChildren<Renderer>()[x].gameObject;
        }

        try
        {
            TextReader read = new StreamReader(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "/Frontier/" + GunName + ".sav");
            currentCamo = read.ReadLine();
            string temp = read.ReadLine();
            switch (temp)
            {
                case "IronSight":
                    currentOptic = optic.IronSight;
                    aimAnimation = IronSightAnim;
                    break;
                case "reflex":
                    currentOptic = optic.reflex;
                    aimAnimation = reflexAnim;
                    break;
                case "ACOG":
                    currentOptic = optic.ACOG;
                    aimAnimation = ACOGAnim;
                    break;
                case "EOTech":
                    currentOptic = optic.EOTech;
                    aimAnimation = EOTechAnim;
                    break;
                case "NVG":
                    currentOptic = optic.NVG;
                    aimAnimation = NVGAnim;
                    break;
                case "RifleScope":
                    currentOptic = optic.RifleScope;
                    aimAnimation = RifeScopeAnim;
                    break;
            }

            string tacTemp = read.ReadLine();
            switch (tacTemp)
            {
                case "none":
                    currentTactic = tactical.none;
                    break;
                case "suppressor":
                    currentTactic = tactical.suppressor;
                    break;
                case "tacticalLight":
                    currentTactic = tactical.tacticalLight;
                    break;
                case "GrenadLauncher":
                    currentTactic = tactical.GrenadLauncher;
                    break;
                case "Bayonet":
                    currentTactic = tactical.Bayonet;
                    break;
            }

            read.Close();
        }
        catch (Exception) { }
        if (usingGun)
        {
            try
            {
                string[] temp = new string[2];
                TextReader readWep = new StreamReader(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "/Frontier/wep.sav");
                temp[0] = readWep.ReadLine();
                temp[1] = readWep.ReadLine();
                readWep.Close();

                if (temp[0] == GunName || temp[1] == GunName)
                {
                    currentAmmo = maxMagAmo * 4;
                }
                else
                    currentAmmo = 0;
            }
            catch (Exception) { currentAmmo = 0; }                
        }
    }

    private void Update()
    {

        foreach(Material c in gameManager.camos)
        {
            if (currentCamo == c.name)
            {
                foreach(GameObject r in rendererObjects)
                {
                    if (r.tag != "skin")
                        r.gameObject.GetComponent<Renderer>().materials = new Material[2] { c, c };
                }
            }
        }

        foreach (Transform scopes in ScopePosition.transform)
        {
            if (currentOptic.ToString() == scopes.name)
            {
                foreach (Transform child in scopes.transform)
                {
                    if (child.name != "camera")
                    {
                        if (child.name == "zoomLookAt")
                        {
                            zoomLookAt = child.gameObject;
                            try
                            {
                                if (usingGun)
                                {
                                    if (player.gameObject.GetComponent<shooting>().Guns[shooting.gunPicked] == gameObject)
                                        child.GetComponent<Renderer>().enabled = true;
                                    else
                                        child.GetComponent<Renderer>().enabled = false;
                                }
                                else
                                    child.GetComponent<Renderer>().enabled = true;
                            }
                            catch (Exception) { }
                        }
                        else
                        {
                            if (usingGun)
                            {
                                if (player.gameObject.GetComponent<shooting>().Guns[shooting.gunPicked] == gameObject)
                                    child.GetComponent<Renderer>().enabled = true;
                                else
                                    child.GetComponent<Renderer>().enabled = false;
                            }
                            else
                                child.GetComponent<Renderer>().enabled = true;
                        }
                    }

                }
            }
            else if (currentOptic.ToString() != scopes.name)
            {
                foreach (Transform child in scopes.transform)
                {
                    if (child.name != "camera")
                    {
                        try
                        {
                            child.GetComponent<Renderer>().enabled = false;
                        }
                        catch (Exception) { }
                    }
                }
            }
        }
        foreach (Transform tac in TacticlePosition.transform)
        {
            if (currentTactic.ToString() == tac.name)
            {
                foreach (Transform child in tac.transform)
                {
                    if (usingGun)
                    {
                        if (player.gameObject.GetComponent<shooting>().Guns[shooting.gunPicked] == gameObject)
                            child.GetComponent<Renderer>().enabled = true;
                        else
                            child.GetComponent<Renderer>().enabled = false;
                    }
                    else
                        child.GetComponent<Renderer>().enabled = true;
                }
            }
            else if (currentTactic.ToString() != tac.name)
            {
                foreach (Transform child in tac.transform)
                {
                    child.GetComponent<Renderer>().enabled = false;
                }
            }
        }
    }
}
